import React, { Component } from 'react';
import './App.css';

import { Route, Switch, Redirect } from 'react-router-dom'
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { actions as userActions } from './redux/modules/user';

import { Home } from './Home';
import LoginForm from './user/Login';

class App extends Component {
  render() {
    let { user } = this.props
    if (user.get('id') === -1) {
      user = null
    }

    return (
      <Switch>
        <Route path="/" exact render={props => (<Redirect to="/new" />)} />
        <Route path="/new" render={props => {
          return (
            user ? 
            <Home 
              {...props}
              user={user}
              defaultSelectedKeys={["1"]} 
            /> 
            : <Redirect to="/login" />
          )
        }}>
        </Route>

        <Route path="/join" render={props => {
          return (
            user ? 
            <Home 
              {...props}
              user={user}
              defaultSelectedKeys={["2"]}
            /> 
            : <Redirect to="/login" />
          )
        }} />

        <Route path="/my" render={props => {
          return (
            user ? 
            <Home 
              {...props}
              user={user}
              defaultSelectedKeys={["3"]}
            /> 
            : <Redirect to="/login" />
          )
        }} />

        <Route path="/login" render={props => 
          (
            <Home 
              {...props}
              user={user}
              content={LoginForm}
              contentStyle={{
                'marginTop': '30px',
                'margin': 'auto'
              }}
              defaultSelectedKeys={["4"]}
            /> 
          )
        } />
      </Switch>
    );
  }
}

const mapStateToProps = state => {
  return {
    user: state.get('user')
  }
}

const mapDispatchToProps = dispatch => {
  return {
    ...bindActionCreators(userActions, dispatch)
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(App);
